import express from 'express'
import adminService from './admin.services'
import { success } from '../../utils/responses'
import {
  adminIdSchema,
  adminPaginationSchema,
  newAdminSchema,
  updateAdminSchema,
} from './admin.validation'

const router = express.Router()

router.post('/', async (req, res) => {
  const newAdmin = newAdminSchema.parse(req.body)
  const admin = await adminService.createAdmin(newAdmin)
  success(res, '管理员新增成功', admin)
})

router.delete('/:id', async (req, res) => {
  // 验证id
  const id = adminIdSchema.parse(req.params.id)
  const admin = await adminService.deleteAdmin(id)
  success(res, '管理员删除成功', admin)
})

router.put('/:id', async (req, res) => {
  // 验证id
  const id = adminIdSchema.parse(req.params.id)
  const updateAdmin = updateAdminSchema.parse(req.body)
  const admin = await adminService.updateAdmin(id, updateAdmin)
  success(res, '管理员更新成功', admin)
})

router.get('/pages', async (req, res) => {
  const adminWithPage = adminPaginationSchema.parse(req.query)
  const pages = await adminService.getAdminPages(adminWithPage)
  success(res, '管理员列表获取成功', pages)
})

router.get('/:id', async (req, res) => {
  // 验证id
  const id = adminIdSchema.parse(req.params.id)
  const admin = await adminService.getAdminById(id)
  success(res, '管理员获取成功', admin)
})
export default router
